package com.windoent.yugioh.ui.activity.zixun;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mobstat.StatService;
import com.just.agentweb.AgentWeb;
import com.windoent.yugioh.R;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.bean.ZiXunDetailsBean;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.ActivityTitle;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import net.nightwhistler.htmlspanner.HtmlSpanner;

import org.json.JSONException;

import okhttp3.Call;

/**
 * author : robert
 * date   : 2021/5/6 18:04
 * desc   :
 */
public class ZiXunDetailActivity extends BaseActivity {
    private TextView mTvTitle;
    private TextView mTvNewsTitle;
    private TextView mTvSees;
    private ImageView mIvSee;
    private TextView mTvTime;
    private TextView mTvAuthor;
    private AgentWeb mAgentWeb;
    private ActivityTitle mActivityTitle;
    private TextView mTvContent;


    @Override
    public int getLayout() {
        return R.layout.activity_zixun_detail;
    }

    @Override
    public void initView() {
        super.initView();
        mActivityTitle = (ActivityTitle) findViewById(R.id.activity_title);
        immersiveStatusBar(mActivityTitle.getView());

        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvNewsTitle = (TextView) findViewById(R.id.tv_news_title);
        mTvSees = (TextView) findViewById(R.id.tv_sees);
        mIvSee = (ImageView) findViewById(R.id.iv_see);
        mTvTime = (TextView) findViewById(R.id.tv_time);
        mTvAuthor = (TextView) findViewById(R.id.tv_author);

        mTvContent = (TextView) findViewById(R.id.tv_content);
    }

    @Override
    public void initData() {
        super.initData();
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        getZiXunDetails(id);
    }

    private void getZiXunDetails(String id) {
        showLoadingDialog("");
        PhoneLive.getZiXunDetails(getUserToken(), id, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            ZiXunDetailsBean json = gson.fromJson(response, ZiXunDetailsBean.class);
                            ZiXunDetailsBean.DataBean data = json.getData();
                            if (data != null) {
                                ZiXunDetailsBean.DataBean.InfoBean info = data.getInfo();
                                if (info != null) {
                                    initUi(info);
                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }

            }
        });
    }

    private void initUi(ZiXunDetailsBean.DataBean.InfoBean info) {
        setText(mTvNewsTitle, info.getTitle());
        setText(mTvTime, info.getTime_desc());
        setText(mTvAuthor, info.getAuthor());
        setText(mTvSees, info.getView() + "");
        String details = info.getDetails();
        if (BaseMethod.notNull(details)) {
            mTvContent.setText(new HtmlSpanner().fromHtml(details));
        }
    }


    @Override
    public void initListener() {
        super.initListener();
        mActivityTitle.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (mAgentWeb != null && mAgentWeb.handleKeyEvent(keyCode, event)) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        if (mAgentWeb != null) {
            mAgentWeb.getWebLifeCycle().onPause();
        }
        super.onPause();
        StatService.onPageEnd(this, "资讯详情界面");
    }

    @Override
    protected void onResume() {
        if (mAgentWeb != null) {
            mAgentWeb.getWebLifeCycle().onResume();
        }
        super.onResume();
        StatService.onPageStart(this, "资讯详情界面");
    }

    @Override
    protected void onDestroy() {
        if (mAgentWeb != null) {
            mAgentWeb.getWebLifeCycle().onDestroy();
        }
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }

}
